package view.purchasing;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.purchasing.OrderDetail;
import utility.com.TextComponentUtils;

public class OrderTM extends AbstractTableModel {
    
    private List<OrderDetail> data;
    private boolean readOnly;

    public OrderTM(List<OrderDetail> data, boolean readOnly) {
        this.data = data;
        this.readOnly = readOnly;
    }

    public OrderTM() {
        this(new ArrayList<OrderDetail>(0), false);
        
    }

    public OrderDetail get(int index) {
        return data.get(index);
    }

    public void add(OrderDetail product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, OrderDetail product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<OrderDetail> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 14;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return (column==1 || column==4 || column==6 ) && readOnly==false;
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 4: 
                BigDecimal qcrt = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setQtyCrt(qcrt);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
                
            case 6: 
                BigDecimal qpcs = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setQtyPcs(qpcs);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;                            
                
            case 3: //END DATE
                fireTableCellUpdated(rowIndex, columnIndex);
                break;       
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
//        if (getRowCount() >= rowIndex) {
            switch (columnIndex) {
                case 0: return rowIndex+1;
                case 1: if (data.get(rowIndex).getItem() != null) {
                            return data.get(rowIndex).getItem().getPlu();
                        } return null;
                case 2: if (data.get(rowIndex).getItem() != null) {
                            return data.get(rowIndex).getItem().getNama();
                        } return null;
                case 4: 
                    String qtyCrtDisplay = TextComponentUtils.formatNumber(data.get(rowIndex).getQtyCrt());
                    return qtyCrtDisplay; 
                case 5: 
                    if(data.get(rowIndex).getUnit()!=null) {
                        return data.get(rowIndex).getUnit().getKode()+"/"+ data.get(rowIndex).getFraksi();
                    } return null;
                case 6: 
                    String qtyPcsDisplay = TextComponentUtils.formatNumber(data.get(rowIndex).getQtyPcs());
                    return qtyPcsDisplay; 
                case 7: 
                    String priceDisplay = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getPrice(), 2);
                    return priceDisplay; 
                case 8: 
                    String diskonDisplay = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getDiskon(), 3);
                    return diskonDisplay; 
                case 9: 
                    String taxDisplay = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getPpn(), 2);
                    return taxDisplay; 
                case 10: 
                    String amountDisplay = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getAmount(), 2);
                    return amountDisplay; 
                case 11: 
                    String qtyBonusDisplay = TextComponentUtils.formatNumber(data.get(rowIndex).getQtyBonus());
                    return qtyBonusDisplay; 
                case 12: 
                    return data.get(rowIndex).getFraksiBonus();
                case 13: 
                    if(data.get(rowIndex).getHadiah()!=null) { return data.get(rowIndex).getHadiah().getNama(); } 
                    return null;
                default: return null;
            }
//        } else {return null; }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "PLU";
            case 2: return "NAMA";
            case 3: return "STOK";
            case 4: return "QTY B";
            case 5: return "ISI";
            case 6: return "QTY K";
            case 7: return "HARGA";
            case 8: return "DISKON";
            case 9: return "PPN";
            case 10: return "JUMLAH";
            case 11: return "QTY BNS";
            case 12: return "FR BNS";
            case 13: return "PRODUK BONUS";
            default: return null;
        }
    }
    
}
